package com.zhuiyun.pm.sys.dictionary.controller;

import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;
import com.zhuiyun.pm.sys.dictionary.entity.DictionaryEntity;
import com.zhuiyun.pm.sys.dictionary.entity.DictionaryEntriesEntity;
import com.zhuiyun.pm.sys.dictionary.entity.DictionaryEntriesTypeEntity;
import com.zhuiyun.pm.sys.dictionary.mapper.DictionaryEntriesMapper;
import com.zhuiyun.pm.sys.dictionary.mapper.DictionaryEntriesTypeMapper;
import com.zhuiyun.pm.sys.dictionary.service.DictionaryEntriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @ClassName DictionaryEntriesController
 * @Description TODO
 * @Author 胡亮
 * @Date 2023/03/14 下午 03:19
 **/
@RestController
@RequestMapping("/dictionaryEntries")
@CrossOrigin
public class DictionaryEntriesController {
    @Autowired
    DictionaryEntriesMapper dictionaryEntriesMapper;
    @Autowired
    DictionaryEntriesTypeMapper dictionaryEntriesTypeMapper;
    @Autowired
    DictionaryEntriesService dictionaryEntriesService;

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/15 上午 09:51
     * @参数1 id
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典项列表(附带数据排序)
     */
    @GetMapping("/dictionaryEntriesSelectAll")
    public CommonResult dictionaryEntriesSelectAll(PageRequest pageRequest, Integer id){
        MyPageHelper.startPage(pageRequest);
        List<DictionaryEntriesEntity> select = dictionaryEntriesMapper.dictionaryEntriesSelectAll(id);
        for (DictionaryEntriesEntity d:select) {
            if (d.getDictionaryEntriesTypeId() != null) {
                DictionaryEntriesTypeEntity dictionaryEntriesType = dictionaryEntriesTypeMapper.selectByPrimaryKey(d.getDictionaryEntriesTypeId());
                d.setDictionaryEntriesTypeName(dictionaryEntriesType.getDictionaryEntriesTypeName());
            }
        }
        PageResult pageResult = MyPageHelper.pageInfo(select);
        return CommonResult.ok(pageResult);
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/15 上午 10:41
     * @参数1 dictionaryEntries
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 删除字典项(逻辑删除附加批量修改排序字段)
     */
    @PostMapping("/dictionaryEntriesDel")
    public CommonResult dictionaryEntriesDel(@RequestBody DictionaryEntriesEntity dictionaryEntries){
        int sort=1;
        System.out.println("\n\n\n 值："+dictionaryEntries.getDictionaryEntriesSort());
        int i = dictionaryEntriesService.dictionaryEntriesDel(dictionaryEntries.getId());
        if(i==1) {
            DictionaryEntriesEntity dictionary = new DictionaryEntriesEntity();
            dictionary.setDictionaryId(dictionaryEntries.getDictionaryId());
            dictionary.setDictionaryEntriesDelFlag(0);
            List<DictionaryEntriesEntity> select = dictionaryEntriesMapper.select(dictionary);
            Collections.sort(select, Comparator.comparingInt(DictionaryEntriesEntity::getDictionaryEntriesSort));
            for (DictionaryEntriesEntity d : select) {
                if(sort<dictionaryEntries.getDictionaryEntriesSort()){
                    sort++;
                }else if(sort>=dictionaryEntries.getDictionaryEntriesSort()){
                    d.setDictionaryEntriesSort(sort);
                    //获取当前时间
                    Date day=new Date();
                    //给创建时间赋值
                    d.setDictionaryEntriesUpdateTime(day);
                    dictionaryEntriesMapper.updateByPrimaryKey(d);
                    sort++;
                }
            }
        }
        return CommonResult.ok(i);
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/15 上午 11:16
     * @参数1 id
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 获取字典项列表长度
     */
    @GetMapping("/sort/{id}")
    public CommonResult sort(@PathVariable Integer id){
        if(id!=null) {
            DictionaryEntriesEntity dictionary = new DictionaryEntriesEntity();
            dictionary.setDictionaryId(id);
            dictionary.setDictionaryEntriesDelFlag(0);
            List<DictionaryEntriesEntity> select = dictionaryEntriesMapper.select(dictionary);
            return CommonResult.ok(select.size()+1);
        }else {
            return CommonResult.ok(null);
        }
    }
    
    /**
     * 
     * @创建人: 胡亮
     * @创建时间: 2023/03/15 上午 11:23 
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典项数据类型列表
     */
    @PostMapping("/dictionaryEntriesTypeSelectAll")
    public CommonResult dictionaryEntriesTypeSelectAll(){
        return CommonResult.ok(dictionaryEntriesTypeMapper.selectAll());
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/15 下午 03:58
     * @参数1 dictionary
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典项新增(附带批量修改)
     */
    @PostMapping("/dictionaryEntriesInsert")
    public CommonResult dictionaryEntriesInsert(@RequestBody DictionaryEntriesEntity dictionary){
        DictionaryEntriesEntity dictionaryEntries = new DictionaryEntriesEntity();
        dictionaryEntries.setDictionaryId(dictionary.getDictionaryId());
        dictionaryEntries.setDictionaryEntriesDelFlag(0);
        List<DictionaryEntriesEntity> select = dictionaryEntriesMapper.select(dictionaryEntries);
        Collections.sort(select, Comparator.comparingInt(DictionaryEntriesEntity::getDictionaryEntriesSort));
        int sort=1;
        for (DictionaryEntriesEntity d:select ) {
            if (sort<dictionary.getDictionaryEntriesSort()){
                sort++;
            }else if(sort>=dictionary.getDictionaryEntriesSort()){
                sort++;
                d.setDictionaryEntriesSort(sort);
                //获取当前时间
                Date day=new Date();
                //给修改时间赋值
                d.setDictionaryEntriesUpdateTime(day);
                dictionaryEntriesMapper.updateByPrimaryKey(d);
            }
        }
        //获取当前时间
        Date day=new Date();
        //给创建时间赋值
        dictionary.setDictionaryEntriesCreateTime(day);
        //给状态赋值(0为未删除)
        dictionary.setDictionaryEntriesDelFlag(0);
        int insert = dictionaryEntriesMapper.insert(dictionary);
        return CommonResult.ok(insert);
    }


    @PostMapping("/dictionaryEntriesUpdate")
    public CommonResult dictionaryEntriesUpdate(@RequestBody DictionaryEntriesEntity dictionary){
        DictionaryEntriesEntity dictionaryEntries = new DictionaryEntriesEntity();
        dictionaryEntries.setDictionaryId(dictionary.getDictionaryId());
        dictionaryEntries.setDictionaryEntriesDelFlag(0);
        List<DictionaryEntriesEntity> select = dictionaryEntriesMapper.select(dictionaryEntries);
        Collections.sort(select, Comparator.comparingInt(DictionaryEntriesEntity::getDictionaryEntriesSort));
        DictionaryEntriesEntity entity = dictionaryEntriesMapper.selectByPrimaryKey(dictionary.getId());
        int sort=1;
        int i=1;
        int I=1;
        System.out.println("\n\n 值："+entity.getDictionaryEntriesSort());
        System.out.println("\n\n 值："+dictionary.getDictionaryEntriesSort());
        if (entity.getDictionaryEntriesSort()>dictionary.getDictionaryEntriesSort()) {
            for (DictionaryEntriesEntity d : select) {
                if(i<dictionary.getDictionaryEntriesSort()){
                    sort++;
                    i++;
                }else if(i==dictionary.getDictionaryEntriesSort()){
                    //获取当前时间
                    Date day = new Date();
                    //给创建时间赋值
                    dictionary.setDictionaryEntriesCreateTime(day);
                    I = dictionaryEntriesMapper.updateByPrimaryKey(dictionary);
                    sort++;
                    d.setDictionaryEntriesCreateTime(day);
                    d.setDictionaryEntriesSort(sort);
                    dictionaryEntriesMapper.updateByPrimaryKey(d);
                    i++;
                    sort++;
                }else if (i>dictionary.getDictionaryEntriesSort()){
                    if((i)<entity.getDictionaryEntriesSort()) {
                        //获取当前时间
                        Date day = new Date();
                        //给创建时间赋值
                        d.setDictionaryEntriesCreateTime(day);
                        d.setDictionaryEntriesSort(sort);
                        dictionaryEntriesMapper.updateByPrimaryKey(d);
                        i++;
                        sort++;
                    }else if((i)==entity.getDictionaryEntriesSort()){
                        break;
                    }
                }
            }
        }else if (entity.getDictionaryEntriesSort()<dictionary.getDictionaryEntriesSort()){
            for (DictionaryEntriesEntity d:select) {
                if (i < entity.getDictionaryEntriesSort()) {
                    sort++;
                    i++;
                } else if (i == entity.getDictionaryEntriesSort()) {
                    i++;
                } else if (i > entity.getDictionaryEntriesSort()) {
                    if (i < dictionary.getDictionaryEntriesSort()) {
                        //获取当前时间
                        Date day = new Date();
                        //给创建时间赋值
                        d.setDictionaryEntriesCreateTime(day);
                        d.setDictionaryEntriesSort(sort);
                        System.out.println("\n 结果："+dictionaryEntriesMapper.updateByPrimaryKey(d));
                        sort++;
                        i++;
                    }else if (i==dictionary.getDictionaryEntriesSort()){
                        //获取当前时间
                        Date day = new Date();
                        //给创建时间赋值
                        d.setDictionaryEntriesCreateTime(day);
                        d.setDictionaryEntriesSort(sort);
                        System.out.println("\n 结果："+dictionaryEntriesMapper.updateByPrimaryKey(d));
                        dictionary.setDictionaryEntriesCreateTime(day);
                        I = dictionaryEntriesMapper.updateByPrimaryKey(dictionary);
                        break;
                    }
                }
            }
        }else if (entity.getDictionaryEntriesSort()==dictionary.getDictionaryEntriesSort()){
            //获取当前时间
            Date day=new Date();
            //给修改时间赋值
            dictionary.setDictionaryEntriesUpdateTime(day);
            I = dictionaryEntriesMapper.updateByPrimaryKey(dictionary);
        }
        return CommonResult.ok(I);
    }
}
